<?php
/**
 * Created by PhpStorm.
 * User: dayu
 * Date: 2019/12/24
 * Time: 14:51
 */

namespace data\service;

use data\model\NsPromotionDiscountGoodsModel;
use data\model\NsPromotionDiscountModel;
use think\Db;

class ActivityGoods extends BaseService
{

    /**
     * 活动商品查找
     * @param $shop_id
     * @param $start_time
     * @param $end_time
     * @param $pag
     * @param int $num
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function discountList($shop_id, $start_time, $end_time, $pag, $num)
    {
        $_discountGoods = new NsPromotionDiscountGoodsModel();
        $goods = new Goods();

        $data = $data['data'] = $_discount_where = $_where = $_discountGoods_where = [];
        $data['count'] = 0;
        $_where['status'] = ['eq', 1];
        $_where['goods_type_crowd'] = ['in', [1, 2]];
        $_where['start_time'] = ['<=', $start_time];
        $_where['end_time'] = ['>=', $end_time];

        $data['count'] = $_discountGoods->where($_where)->count();
        $goods_id_array = $_discountGoods->where($_where)->page($pag, $num)->field('goods_id,goods_type_crowd,shop_id')->select();
        foreach ($goods_id_array as $k => $v) {
            $data['data'][$k] = $goods->miniatureGoodsInfo($v['goods_id']);
            $data['data'][$k]['goods_type_crowd'] = $v['goods_type_crowd'];
            $data['data'][$k]['type'] = ($shop_id == $v['shop_id']) ? 0 : 1;
        }
        return $data;
    }
// public function discountList($shop_id, $start_time, $end_time, $pag, $num)
//    {
//        $_discount = new NsPromotionDiscountModel();
//        $_discountGoods = new NsPromotionDiscountGoodsModel();
//        $goods = new Goods();
//
//        $data = $data['data'] =$_discount_where=$_where = $_discountGoods_where = [];
//        $data['count'] = 0;
//        $_discount_where['status']=['eq',1];
//        $_discount_where['shop_id']=['eq',$shop_id];
//        $_discount_where['examine_status']=['eq',1];
//        $_discount_where['start_time']=['between', [$start_time, $end_time]];
//        $discountIdList = $_discount->where($_discount_where)->field('discount_id')->select();
//        if ($discountIdList) {
//            $discountIdList = array_column($discountIdList, 'discount_id');
//            $_where['discount_id']=['in',$discountIdList];
//            $_where['goods_type_crowd']=['in',[1,2]];
//            $data['count'] = $_discountGoods->where($_where)->count();
//            $goods_id_array = $_discountGoods->where($_where)->page($pag, $num)->field('goods_id,goods_type_crowd')->select();
//
//            foreach ($goods_id_array as $k => $v) {
//                $data['data'][$k] = $goods->miniatureGoodsInfo($v['goods_id']);
//                $data['data'][$k]['goods_type_crowd'] = $v['goods_type_crowd'];
//            }
//        }
//        return $data;
//    }

}